package app.itgungnir.compose.wan.widgets

import android.content.res.Configuration
import androidx.compose.foundation.layout.Column
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Alarm
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import app.itgungnir.compose.wan.theme.WanComposePreviewTheme
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.pagerTabIndicatorOffset
import com.google.accompanist.pager.rememberPagerState
import kotlinx.coroutines.launch

open class TabItem(val icon: ImageVector? = null, val title: String? = null, val screen: @Composable () -> Unit)

@OptIn(ExperimentalPagerApi::class, ExperimentalMaterialApi::class)
@Composable
fun WanTabLayout(
    modifier: Modifier = Modifier,
    tabs: List<TabItem> = listOf(),
    backgroundColor: Color = MaterialTheme.colors.background,
    contentColor: Color = MaterialTheme.colors.primary,
    scrollable: Boolean = false,
    scrollEdgePadding: Dp = 0.dp
) {
    Column(modifier = modifier) {
        val pagerState = rememberPagerState()
        val coroutineScope = rememberCoroutineScope()
        if (scrollable) {
            ScrollableTabRow(
                selectedTabIndex = pagerState.currentPage,
                indicator = { tabPositions ->
                    TabRowDefaults.Indicator(
                        Modifier.pagerTabIndicatorOffset(pagerState = pagerState, tabPositions = tabPositions)
                    )
                },
                backgroundColor = backgroundColor,
                contentColor = contentColor,
                edgePadding = scrollEdgePadding
            ) {
                tabs.forEachIndexed { index, tabItem ->
                    LeadingIconTab(
                        selected = pagerState.currentPage == index,
                        icon = { tabItem.icon?.let { Icon(imageVector = it, contentDescription = "") } },
                        text = { tabItem.title?.takeIf { it.isNotEmpty() }?.let { Text(text = it) } },
                        onClick = {
                            coroutineScope.launch {
                                pagerState.animateScrollToPage(index)
                            }
                        }
                    )
                }
            }
        } else {
            TabRow(
                selectedTabIndex = pagerState.currentPage,
                indicator = { tabPositions ->
                    TabRowDefaults.Indicator(
                        Modifier.pagerTabIndicatorOffset(pagerState = pagerState, tabPositions = tabPositions)
                    )
                },
                backgroundColor = backgroundColor,
                contentColor = contentColor
            ) {
                tabs.forEachIndexed { index, tabItem ->
                    LeadingIconTab(
                        selected = pagerState.currentPage == index,
                        icon = { tabItem.icon?.let { Icon(imageVector = it, contentDescription = "") } },
                        text = { tabItem.title?.takeIf { it.isNotEmpty() }?.let { Text(text = it) } },
                        onClick = {
                            coroutineScope.launch {
                                pagerState.animateScrollToPage(index)
                            }
                        }
                    )
                }
            }
        }
        HorizontalPager(count = tabs.size, state = pagerState) { page ->
            tabs[page].screen()
        }
    }
}

@Preview(name = "WanTabLayout light mode", showBackground = false)
@Preview(name = "WanTabLayout dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = false)
@Composable
private fun WanTabLayoutPreview() {
    WanComposePreviewTheme {
        val tabs = listOf(
            TabItem(icon = Icons.Filled.Alarm, title = "Tab One") {},
            TabItem(icon = Icons.Filled.Alarm, title = "Tab Two") {}
        )
        WanTabLayout(tabs = tabs)
    }
}
